package com.playball.lottery.util;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.playball.lottery.ApplicationData;
import com.playball.lottery.nt.GetRequestParams;

import android.R.integer;
import android.content.Context;
import android.os.AsyncTask;
import android.util.Log;


/**
 * 倍投的算法
 * 
 * @ClassName: IntelligenceChaseUtil
 * @Description: TODO(这里用一句话描述这个类的作用)
 * @author 2w
 * @date 2014年7月23日 下午5:20:21
 * 
 */
public class IntelligenceChaseUtil {

	/** 投入注数 */
	private int castCount = 1;
	/** 投入期数 */
	private int castIsuse ;
	/** 起始倍数 1 */
	private int startMutiple = 1  ;
	/** 单注奖金 */
	private int perAward;
	/** 期望收益 */
	// 累计盈利不低于
	private int expectIncome;
	/** 期望收益率 */
	// 全程盈利率
	private int expectIncomeRate;

	/** 当前期数 */
	private int currentIsuse = 1;
	/** 投入倍数=起始倍数 */
	private int castMutiple;
	/** 本期投入=投入注数*投入倍数*2 */
	private int currentCost;
	/** 累计投入=本期投入 */
	private int totalCost;
	/** 奖金=投入倍数*单注奖金 */
	private int totalAward;
	/** 累计收益=投入倍数*单注奖金-累计投入 */
	private double totalIncome;
	/** 收益率= 累计收益/累计投入*100 */
	private double incomeRate;

	/** 本期投入 临时变量 */
	private int tmpCurrentCost;
	/** boolean */
	private boolean tmpFlag;

	/** 选择的类型 */
	// 2. 累计盈利不低于 1. 全程盈利率
	private int model;
	
	/**当前期次*/
	private String issueName ;
	/**取出后面的2位 转换 整数*/
	private int issueInt ;

	/** 66 快三 */
	private int lorID = 66;
	

	private List<Map<String, Object>> dataList = new ArrayList<Map<String, Object>>();
	
	private List<Map<String, Object>> list_issue  ;
	
	
	private Context  mContext ;
	
	
	private int intIssue = 0;

	public IntelligenceChaseUtil() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @param issueName
	 *            期次
	 * @param castCount
	 *            投入倍数
	 * @param castIsuse
	 *            投入期数
	 * @param lorID
	 *            彩种ID
	 * @param perAward
	 *            单注奖金
	 * @param model
	 *            选择类型
	 * @param expectIncome
	 *            累计盈利率不低于
	 * @param expectIncomeRate
	 *            全程盈利率
	 */
	public IntelligenceChaseUtil(Context  mContext ,String issueName ,int castCountMul ,int castIsuse, int lorID, int perAward,
			int model, int expectIncome, int expectIncomeRate) {
		super();
		this.mContext = mContext ;
		this.issueName  =  issueName ;
		this.startMutiple  = castCountMul ;
		this.castIsuse = castIsuse;
		this.lorID = lorID;
		this.perAward = perAward;
		this.model = model;
		this.expectIncome = expectIncome;
		this.expectIncomeRate = expectIncomeRate;
		//getIssue();
	}
	
	public void setData(List<Map<String, Object>> list_issue) {
		this.list_issue = list_issue ;
	}
	
	private void initData() {
		try {
				castMutiple = startMutiple; // 投入倍数=起始倍数
				currentCost = castCount * castMutiple * 2; // 本期投入=投入注数*投入倍数*2
				totalCost = currentCost;// 累计投入=本期投入
				totalAward = castMutiple * perAward;// 奖金=投入倍数*单注奖金
				totalIncome = castMutiple * perAward - totalCost; // 累计收益=投入倍数*单注奖金-累计投入
				incomeRate = totalIncome / totalCost * 100;// 收益率= 累计收益/累计投入*100
				tmpCurrentCost = currentCost; // 本期投入 临时变量
			
		/*	if (issueName.length()==2) {
				issueInt = Integer.parseInt(issueName);
			}else {
				issueName = issueName.substring(issueName.length()-2, issueName.length());
				issueInt = Integer.parseInt(issueName);
			}*/
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public List<Map<String, Object>> getDataList() throws Exception {
		initData();
		dataList = method() ;
		return dataList;
	}
	
	
	
	private List<Map<String, Object>>   method(){
		int i = 0;
		while (currentIsuse <= castIsuse) {
			switch (model) {
			// 累计盈利不低于
			case 2:
				// 如果选择累计收益，当累计收益>=期望收益时，tmpFlag为true,反之false;
				if (totalIncome >= expectIncome) {
					tmpFlag = true;
				} else {
					tmpFlag = false;
				}

				break;
			// 全程盈利率
			case 1:
				// 如果选择收益率，当收益率>=期望收益率时，tmpFlag为true;反之false;
				if (incomeRate >= expectIncomeRate) {
					tmpFlag = true;

				} else {
					tmpFlag = false;
				}
				break;
			default:
				break;
			}
			// 循环多少次
			int len = castIsuse;
			if (tmpFlag && i < len) {
				currentIsuse++;
				if (model != 4) {
					castMutiple = startMutiple - 1; // ?
				}
				tmpCurrentCost = 0; // ?
				i++;
			}
			if (model == 4) {
				// castMutiple += increaseMutiple;
			} else {
				castMutiple++;
			}
			if (tmpFlag) {
				Map<String, Object> map = new HashMap<String, Object>();
				 //快3
				if (lorID == Constants.LOT_ID_KUAI3) {
					//int issueK3Max = issueInt++ ;
				    int k =	intIssue ++ ;
					if ( k <= Constants.ISSUE_K_3_MAX  ) {
						//Log.d("van",list_issue.get(k).get("IssueName").toString() );
						//map.put("issueName", String.format("%02d", issueK3Max));
						String IssueName = list_issue.get(k).get("IssueName").toString() ;
						issueName = IssueName.substring(IssueName.length()-2, IssueName.length());
						map.put("issueName", issueName );
						map.put("IssueID", list_issue.get(k).get("IssueID").toString());
						map.put("castMutiple", currentCost/2);//倍数
						map.put("currentCost", currentCost);//本期投入
						map.put("totalCost", totalCost);//累计投入
						map.put("totalAward", totalAward);//奖金
						map.put("totalIncome", totalIncome);//累计收益
						map.put("incomeRate", incomeRate);//收益率
						dataList.add(map);
					}
				 
				}
				//11选5
				else if (lorID == Constants.LOT_ID_11X5) {
					//int issue11_5Max = issueInt++ ;
					int k =	intIssue ++ ;
					if ( k <= Constants.ISSUE_11_5_MAX  ) {
						//map.put("issueName", String.format("%02d", issue11_5Max));
						String IssueName = list_issue.get(k).get("IssueName").toString() ;
						issueName = IssueName.substring(IssueName.length()-2, IssueName.length());
						map.put("issueName", issueName );
						map.put("IssueID", list_issue.get(k).get("IssueID").toString());
						map.put("castMutiple", currentCost/2);//倍数
						map.put("currentCost", currentCost);//本期投入
						map.put("totalCost", totalCost);//累计投入
						map.put("totalAward", totalAward);//奖金
						map.put("totalIncome", totalIncome);//累计收益
						map.put("incomeRate", incomeRate);//收益率
						dataList.add(map);
					}
				}
				// 11运 夺金
				else if (lorID == Constants.LOT_ID_11YDJ) {
					//int issue11_DJMax = issueInt++ ;
					int k =	intIssue ++ ;
					if ( k <= Constants.ISSUE_11_DJ_MAX  ) {
						//map.put("issueName", String.format("%02d", issue11_DJMax));
						String IssueName = list_issue.get(k).get("IssueName").toString() ;
						issueName = IssueName.substring(IssueName.length()-2, IssueName.length());
						map.put("issueName", issueName );
						map.put("IssueID", list_issue.get(k).get("IssueID").toString());
						map.put("castMutiple", currentCost/2);//倍数
						map.put("currentCost", currentCost);//本期投入
						map.put("totalCost", totalCost);//累计投入
						map.put("totalAward", totalAward);//奖金
						map.put("totalIncome", totalIncome);//累计收益
						map.put("incomeRate", incomeRate);//收益率
						dataList.add(map);
					}
				 
				}
				// 重庆时时彩
				else if (lorID == Constants.LOT_ID_CQSSC) {
					//int issue11_CQMax = issueInt++ ;
					int k =	intIssue ++ ;
					if ( k <= Constants.ISSUE_CQSS_MAX  ) {
						String IssueName = list_issue.get(k).get("IssueName").toString() ;
						issueName = IssueName.substring(IssueName.length()-2, IssueName.length());
						int isu = Integer.parseInt(issueName);
						if (isu > 99) {
							//map.put("issueName", String.format("%03d", issue11_CQMax));
							map.put("issueName", IssueName.substring(IssueName.length()-3, IssueName.length()));
						}else {
							map.put("issueName", issueName);
						}
						map.put("IssueID", list_issue.get(k).get("IssueID").toString());
					    map.put("castMutiple", currentCost/2);//倍数
						map.put("currentCost", currentCost);//本期投入
						map.put("totalCost", totalCost);//累计投入
						map.put("totalAward", totalAward);//奖金
						map.put("totalIncome", totalIncome);//累计收益
						map.put("incomeRate", incomeRate);//收益率
						dataList.add(map);
					}
				}
		
			}

			currentCost = castCount * castMutiple * 2; // 本期投入=投入注数*投入倍数*2
		    totalCost = totalCost - tmpCurrentCost + currentCost;// 累计投入
			tmpCurrentCost = currentCost;
			totalAward = castMutiple * perAward; // 奖金=投入倍数*单注奖金
			totalIncome = castMutiple * perAward - totalCost; // 累计收益=投入倍数*单注奖金-累计投入
			incomeRate = totalIncome / totalCost * 100; // 收益率= 累计收益/累计投入*100

			if (castMutiple > 99999) {
				System.out.println("您的倍投----------"+castMutiple);
				// showMessage('您的方案不适合倍投');
				System.out.println("您的设置只适合倍投" + (currentIsuse - 1) + '期');
				dataList = null;
				return dataList;
			}

		}
		
		return dataList;
	}
	
	
	/**************************************获取期次*******/
	/*private void getIssue(){
		AsyncLoader taskIssure = new AsyncLoader();
		String[] dyn = new String[2];
		JSONObject params = new JSONObject();
		try {
			params.put("LottID", lorID);
			params.put("ChaseCount", castIsuse);
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		dyn[0] = params.toString();
		dyn[1] = "2081";
		taskIssure.execute(dyn);
	}
	
	class AsyncLoader extends AsyncTask<String, Integer, String> {
		@Override
		protected String doInBackground(String... params) {
			String strResult = null;
			String action = params[1];
			// 获取请求返回值
			String UserID = ApplicationData.getInstance().getUserID();
			strResult = GetRequestParams.getRequestUserIDResult(params[0], UserID, action, mContext);
			// 获取请求返回值
			return strResult;
		}
		
		@Override
		protected void onPostExecute(String result) {
			// TODO Auto-generated method stub
			super.onPostExecute(result);
			if (result!=null) {
			 list_issue = new ArrayList<Map<String, Object>>();
				try {
					JSONArray js = new JSONArray(result);
					for(int i =0;i<js.length();i++){
						Map<String, Object> map = new HashMap<String, Object>() ;
						JSONObject json = new JSONObject(js.getString(i));
						map.put("IssueID", json.getString("ID"));
						map.put("IssueName", json.getString("Name"));
						list_issue.add(map) ;
					}
				
				} catch (Exception e) {
					e.printStackTrace();
				}
			}else {
			}
		}
	}*/
	
}
